﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HHDBUtility;
namespace HHWeb.Handler
{
    /// <summary>
    /// TreePagePower 的摘要说明
    /// </summary>
    public class TreePagePower : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string rd = context.Request.QueryString["rd"].ToString();
            context.Response.Write(GetPagePowers());
        }
        public string GetPagePowers()
        {
            DataSet ds=DbHelperSQL.Query("SELECT ID,PagePowerName,ParentID FROM PagePower WITH(NOLOCK) WHERE Openstate=1 ORDER BY sort DESC");
            return DataTableToJson("TreeJson", ds.Tables[0]);
        }
        #region  把DataTable数据集转化成Json数据
        /// <summary>
        /// 把DataTable数据集转化成Json数据
        /// </summary>
        /// <param name="jsonName">Json名称</param>
        /// <param name="dt">数据集</param>
        /// <returns></returns>
        public string DataTableToJson(string jsonName, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }
        #endregion     
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}